//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************
#include "globals.h"

#define LOCAL_ERR(fmt,...) \
{\
void *sysname=(void*)udev_device_get_sysname(\
registry[c->output_name].output.d);\
PERR("cfg:output:%s:mode:" fmt,sysname,##__VA_ARGS__);\
}

#define LOCAL_LOG(fmt,...) \
{\
void *sysname=(void*)udev_device_get_sysname(\
registry[c->output_name].output.d);\
LOG("cfg:output:%s:mode:" fmt,sysname,##__VA_ARGS__);\
}

#if LOG_CFG_LVL>=2
#define LOCAL_LOG_LVL2(fmt,...) LOCAL_LOG(fmt,##__VA_ARGS__);
#else
#define LOCAL_LOG_LVL2 (void)0;
#endif

#if LOG_CFG_LVL>=2
#define LOCAL_LOG_LVL2_DUMP_LINE \
{\
u8 line_buf[129];\
memset(&line_buf[0],0,129);\
u8 sz=line_end-line_start;\
memcpy(&line_buf[0],c->c.m+line_start,sz>128?128:sz);\
LOCAL_LOG("%ld \"%s\"\n",line_start,&line_buf[0]);\
}
#else
#define LOCAL_LOG_LVL2_DUMP_LINE
#endif

#define MODE_KEY_STR "mode"
#define ROW_INDEX_KEY_STR "row_index"
#define X_KEY_STR "x"
#define Y_KEY_STR "y"

#define IS_KEY(x) (val_start-1-key_start)==cs_n(x ## _KEY_STR)\
&&!strncmp(c->c.m+key_start,x ## _KEY_STR,val_start-1-key_start)
void cfg_output_mode_get(struct cfg_ctx_output *c,s64 output_start,void *mode)
{
  s64 line_start=output_start;
  while(1){
    if((u64)line_start>=c->c.sz){
      LOCAL_LOG_LVL2("no more mode key/value:line start not in file\n");
      break;
    }

    s64 line_end=cfg_line_end_reach(&c->c,line_start);
    LOCAL_LOG_LVL2_DUMP_LINE;

    s64 key_start=cfg_blanks_skip(&c->c,line_start,line_end);

    //finished:beginning of next section reached ("[key]")
    if(*(u8*)(c->c.m+key_start)=='[') break;

    s64 val_start=cfg_key_skip_to_val(&c->c,key_start,line_end);
    if(val_start==CFG_KEY_SKIP_TO_VAL_NO_VAL){
      LOCAL_LOG_LVL2("no value\n");
    }else if(IS_KEY(MODE)){
      LOCAL_LOG_LVL2("mode key/value found\n");
      s64 val_end=cfg_non_blanks_skip(&c->c,val_start,line_end);
      u64 val_sz=val_end-val_start;
      if(val_sz<=sizeof("hhhhxvvvv@rrr")-1)
        strncpy(mode,c->c.m+val_start,val_sz);
    }

    line_start=line_end+1;
  }
  LOCAL_LOG("mode='%s'\n",mode);
}

